package cn.jy.careuplusdoctor.mycenter;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import cn.jy.careuplusdoctor.R;
import cn.jy.careuplusdoctor.base.BaseActivity;
import cn.jy.careuplusdoctor.entity.Doctor;
import cn.jy.careuplusdoctor.http.HttpHelper;
import cn.jy.careuplusdoctor.http.HttpUrls;
import cn.jy.careuplusdoctor.http.HttpHelper.onHttpListener;
import cn.jy.careuplusdoctor.injector.IdInject;
import cn.jy.careuplusdoctor.response.LoginResponse;
import cn.jy.careuplusdoctor.utils.StringUtil;
import cn.jy.careuplusdoctor.widgets.CareuTitleBar.TitleBarLeftAction;

/**
 * 修改密码
 */
public class ResetPasswordActivity extends BaseActivity implements TitleBarLeftAction{
	// 旧密码
	@IdInject(id = R.id.edt_old_password)
	private EditText	edt_old_password;
	// 新密码
	@IdInject(id = R.id.edt_new_password)
	private EditText	edt_new_password;
	// 确认新密码
	@IdInject(id = R.id.edt_confirm_password)
	private EditText	edt_confirm_password;
	// 提交
	@IdInject(id = R.id.btn_commit)
	private EditText	btn_commit;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_reset_password);
		initTitleBar();
		onInJectView();
	}
//	@Override
//	public void setContentView(int layoutResID) {
//		// TODO Auto-generated method stub
//		super.setContentView(layoutResID);
//		setContentView(R.layout.activity_reset_password);
//	}
	@Override
	protected void settingTitleBar() {
		// TODO Auto-generated method stub
		super.settingTitleBar();
		getTitleBar().setTitle("修改密码");
		getTitleBar().setLeftText("更多");
		getTitleBar().getRighTextView().setVisibility(View.GONE);
		getTitleBar().getLeftImageView().setImageResource(R.drawable.title_bar_back);
		getTitleBar().setLeftAction(ResetPasswordActivity.this);
	}

	@Override
	protected void onAfterInJector() {
		super.onAfterInJector();
		OnClickListener listener = new OnClickListener() {
			@Override
			public void onClick(View v) {
				switch (v.getId()) {
					case R.id.btn_commit:
						estimate();
						break;
				}
			}
		};
		findViewById(R.id.btn_commit).setOnClickListener(listener);
	}

	/**
	 * 判断条件，符合就进入修改程序
	 */
	public void estimate() {
		if (!StringUtil.isNotEmpty(edt_old_password.getText().toString())) {
			longToast("旧密码与新密码不能为空");
			return;
		} else if (!StringUtil.isNotEmpty(edt_new_password.getText().toString())) {
			longToast("旧密码与新密码不能为空");
			return;
		}
		// else if
		// (!edt_new_password.getText().toString().equals(edt_old_password.getText().toString()))
		// {
		// Toast.makeText(ResetPasswordActivity.this, "新密码两次输入不一致",
		// Toast.LENGTH_SHORT).show();
		// return;
		// }
		else if (!edt_new_password.getText().toString().equals(edt_confirm_password.getText().toString())) {
			longToast("新密码两次输入不一致");
			return;
		} else if (6 > edt_new_password.getText().toString().length()
				|| edt_new_password.getText().toString().length() > 20) {
			longToast("新密码长度不能少于6位");
			return;
		}
		commitPassword();
	}

	/**
	 * 开始实行提交功能
	 */
	public void commitPassword() {
		showLoading("正在提交");
		final SharedPreferences shared = getSharedPreferences("careu_doctor",  //缓存
				Activity.MODE_PRIVATE);
		String doctorId = shared.getString("doctorId", "");
		final String new_password = edt_new_password.getText().toString();
//		String doctorId = "1";
		HttpHelper.getInstance().resetPassword(doctorId,edt_old_password.getText().toString(),new_password, new onHttpListener<LoginResponse>() {
			@Override
			public void onResponse(LoginResponse data, int code) {
				hidenLoading();
				if (data != null && code == HttpUrls.REQUEST_SUCCESS) {
					if (TextUtils.equals("1", data.getResType())) {
						shortToast(data.getMsg());
						defaultFinish();
						
						Editor edit = shared.edit();
						edit.putString("loginPassword", new_password);
						edit.commit();
					} else {
						shortToast(data.getMsg());
					}
				} else {
					shortToast("网络故障，修改失败");
				}
			}
		});
	}
	@Override
	public void onLeftPress(View view) {
		defaultFinish();
	}
}
